package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.ClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    /**
     * 增加班级
     * debug:token过期；className唯一约束；添加时sql语句未设置创建时间、更新时间
     * @param req
     * @return
     */
    @PostMapping
    public Result addClass(@RequestBody ClassAddReq req) {
        log.info(req.toString());
        Class1 cls = new Class1();
        cls.setHeadTeacher(req.getEmp().getId());
        cls.setClassName(req.getClassName());
        cls.setRoomName(req.getRoomName());
        cls.setStartTime(req.getStartTime());
        cls.setEndTime(req.getEndTime());
        cls.setCreateTime(LocalDateTime.now());
        cls.setUpdateTime(LocalDateTime.now());

        Boolean hasAdded = classService.addClass(cls);
        if (hasAdded) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id数组删除
     * debug:删除使用事务，抛出异常给全局异常处理器处理；使用动态sql实现
     * @param ids
     * @return
     * @throws Exception
     */
    @DeleteMapping("{ids}")
    public Result deleteClass(@PathVariable("ids") String[] ids) throws Exception {
        Boolean hasDeleted = classService.deleteByIds(ids);
        if (hasDeleted) {
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 分页查询 -查询全部也是这个按钮
     * debug:使用PageHelper，流程先set再查询出集合，再把集合强转为info格式的，最后get出来封装到PageBean；
     * 动态sql的<where><if>等
     * @param page
     * @param pageSize
     * @param className
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping
    public Result getAllClass(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              String className,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTime,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime)
    {
        PageBean pb = classService.getAllClass(page, pageSize, className, beginTime, endTime);
        return Result.success(pb);
    }

    /**
     * 更新班级信息
     * debug:<set>后面的<if>每一个逗号还是要加的；注意更新时间
     * @param req
     * @return
     * @throws Exception
     */
    @PutMapping
    public Result updateClass(@RequestBody ClassAddReq req) throws Exception {
        log.info(req.toString());
        Class1 cls = new Class1(req.getId(),req.getClassName(),req.getRoomName(),
                req.getStartTime(),req.getEndTime(),req.getEmp().getId(),
                LocalDateTime.now(),LocalDateTime.now());
        log.info(cls.toString());
        Boolean hasUpdated = classService.updateClass(cls);
        if (hasUpdated) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }

    }
}
